package cn.wd.service.impl;

import cn.wd.page.PageResult;
import cn.wd.service.IVideoService;
import cn.wd.domain.Video;
import cn.wd.mapper.VideoMapper;
import cn.wd.qo.QueryObject;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class VideoServiceImpl implements IVideoService {

    private final VideoMapper videoMapper;

    public VideoServiceImpl(VideoMapper videoMapper) {
        this.videoMapper = videoMapper;
    }

    @Override
    public Video getById(String videoId) {
        return videoMapper.selectByPrimaryKey(videoId);
    }

    @Override
    public void changeStatus(Long id, String status) {
        videoMapper.changeStatus(id,status);
    }

    @Override
    public void updateById(Video video) {
        videoMapper.updateByPrimaryKey(video);
    }

    @Override
    public void save(Video video) {
        videoMapper.insert(video);
    }

    @Override
    public void deleteById(Long id, boolean deleted) {
        videoMapper.deleteByPrimaryKey(id,deleted);
    }

    @Override
    public PageResult<Video> query(QueryObject qo) {
        Long total = videoMapper.selectForCount(qo);
        if (total == 0) {
            return PageResult.empty(qo.getCurrentPage(), qo.getPageSize());
        }
        List<Video> videos = videoMapper.selectForList(qo);
        return new PageResult<>(qo.getCurrentPage(), qo.getPageSize(), total, videos);
    }
}
